﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Gallio.Framework;
using MbUnit.Framework;
using Netkk.Common.Models;
using Netkk.Library.Repositories;
using Netkk.Library.Services;
using Netkk.Library.Validation;

namespace Netkk.Tests.Appointments
{
    [TestFixture]
    public class AppointmentsTest
    {
        private IAppointmentService appointmentService;

        [SetUp]
        public void Startup()
        {
            ModelStateDictionary modelStateDictionary = new ModelStateDictionary();
            IValidationDictionary validationDictionary = new ModelStateWrapper(modelStateDictionary);
            appointmentService = new AppointmentService(validationDictionary, new AppointmentRepository());
        }

        [TearDown]
        public void Shutdown()
        {
        }

        [Test]
        public void Loads_The_Latest_Appointment()
        {
            Appointment appointment = appointmentService.GetLatestAppointment();
            Assert.GreaterThanOrEqualTo(appointment.Date, DateTime.Now);
        }

        [Test]
        [Row("{e3adad30-1122-494b-9a31-bc0326da1e39}")]
        [Row("{317db095-f616-4b79-b295-4e253b82c40d}")]
        public void Schould_Return_A_Valid_Appointment_From_A_Given_Id(string guid)
        {
            Appointment appointment = appointmentService.GetAppointmentById(new Guid(guid));

            Assert.IsNotNull(appointment);
        }

        [Test]
        [Row("{77777777-03f4-4f69-847e-d0412734a7a9}")]
        [Row("{52296b22-c5b8-4894-b628-777777777777}")]
        [Row("{6010b21c-7777-7777-7777-f621caf0f9bc}")]
        public void Schould_Not_Return_A_Valid_Appointment_From_A_Given_Id(string guid)
        {
            Appointment appointment = appointmentService.GetAppointmentById(new Guid(guid));

            Assert.IsNull(appointment);
        }

        [Test]
        public void Loads_The_Next_Appointments()
        {
            IEnumerable<Appointment> appointments = appointmentService.GetNextAppointments();

            foreach (Appointment appointment in appointments)
            {
                Assert.GreaterThanOrEqualTo(appointment.Date, DateTime.Now);
            }
        }

        [Test]
        public void Loads_The_Past_Appointments()
        {
            IEnumerable<Appointment> appointments = appointmentService.GetPastAppointments();

            foreach (Appointment appointment in appointments)
            {
                Assert.GreaterThan(DateTime.Now, appointment.Date);
            }
        }

        [Test]
        public void Create_Select_And_Delete_A_User()
        {
            Guid appId = new Guid();
            Appointment user = new Appointment
                               {
                                   Id = appId,
                                   Date = DateTime.Now.AddDays(7),
                                   Description = "Hallo Welt",
                                   Location = "Hier",
                                   ShortTitle = "Hallo",
                                   Teaser = "Welt",
                                   Title = "HW"
                               };

            Assert.IsTrue(appointmentService.SaveAppointment(user), "Termin wurde nicht erstellt");

            user = appointmentService.GetAppointmentById(appId);

            Assert.IsNotNull(user, "Erstelter Termin wurde nicht gefunden");

            appointmentService.DeleteAppointment(user);

            user = appointmentService.GetAppointmentById(appId);

            Assert.IsNull(user, "Erstellter Termin wurde nicht gelöscht");
        }

    }
}
